/******************************************************************* 
 *  Copyright(c) 2000-2013 
 *  All rights reserved. 
 *   
 *  文件名称:    DialServicesSDK.h
 *  简要描述:  
 *   
 *  创建日期: 
 *  作者: 江浩
 *  说明: 
 *   
 *  修改日期: 
 *  作者: 
 *  说明: 
 ******************************************************************/  



#include <string>
#include <sys/types.h>
#include <unistd.h>
#include <pthread.h>
#include <mosquitto.h>
#include <mqtt_protocol.h>

using namespace std;

typedef void (*ServicsFunc)(char *, int , char **, int *);  //定义类型
typedef void (*ServicsResp)(char *);  //定义类型

#define CN_DAILUNUSED(A) 			(void)(A)
#define CN_DIALMSG_MAXLEN          (32*1024)


#define CN_DIALERRNO_SUCCESS       (0)
#define CN_DIALERRNO_PARAMERR      (1)
#define CN_DIALERRNO_DONOTUSJING   (2)
#define CN_DIALERRNO_UNABLEALLOC   (3)
#define CN_DIALERRNO_UNINIT        (4)
#define CN_DIALERRNO_SERVINGERR    (5)
#define CN_DIALERRNO_CONNECTERR    (6)
#define CN_DIALERRNO_REQERR        (7)
#define CN_DIALERRNO_RESPERR       (8)
#define CN_DIALERRNO_TIMEOUT       (9)


class CDialServicerAPI
{
public:
	
	CDialServicerAPI();
	~CDialServicerAPI();
	int ServiceStart(const string servName, ServicsFunc userServFun, ServicsResp userServResp=NULL
		, string brokerIp="52.28.72.253",  int brokerPort=1883);
public:
	string  GetServName();
	ServicsFunc GetServFun();	
	ServicsResp GetServResp();
	string GetServBrokerAddr();
	int GetServbrokerPort();
	int GetQos();
private:
	int ServiceSet(const string servName, ServicsFunc userServFun, ServicsResp userServResp=NULL
		, const string brokerIp="52.28.72.253", const int brokerPort=1883);
private:
	int     			m_qos;
	int 				m_brokerPort;
	string  			m_strBrokerAddr;
	string				m_servName;
	struct mosquitto * 	m_pMosq;
	ServicsFunc			m_userServFun;
	ServicsResp 		m_userServResp;
};


class CDialClientAPI
{
public:
	CDialClientAPI();
	~CDialClientAPI();
	int ClientStart(const string clientName, const string brokerIp="52.28.72.253", const int brokerPort=1883);
    int Calling(const string servName, char *request, int requestLen, char *responses, int *responsesLen, int timeOutMs=1000);
	int Request(const string servName, char *request, int requestLen);
	int WaitResponse(const string servName, char *responses, int *responsesLen, int timeOutMs=1000);
public:
	int  GetInvokeID();
	void SetRespBuf(char * pRespBuf , int responsesLen, unsigned int invokeIDFromMsg);
	void GetRespBuf(char	* pRespBuf , int *responsesLen, unsigned int *invokeIDFromMsg);
	void Notify();
	string GetClientName();
	int  GetQos();
private:
	void Receive ();
	void AddInvokeID();
private:
	
	int     			m_qos;
	int 				m_brokerPort;
	int        	 		m_pipFiledes[2];
	string  			m_strBrokerAddr;
	string				m_clientName;
	struct mosquitto    *m_pMosq;
	pthread_mutex_t 	m_mutex;
    /*需要锁保护的变量(需要暴露在全局回调函数及类成员函数中进行读写的变量) 开始*/
	unsigned int 		m_invokeID;
	char    			*m_pRespBuf;
	int                 m_ResponsesLen;
	int                 m_invokeIDFromMsg;
    /*需要锁保护的变量(需要暴露在全局回调函数及类成员函数中进行读写的变量) 结束*/
};
